package com.lbssoft.gpsbd.common;

import com.alibaba.fastjson.JSONObject;
import com.lbssoft.datalink.util.Log;
import com.lbssoft.datalink.util.Parser;
import com.lbssoft.datalink.util.PatternBuilder;
import com.lbssoft.gpsbd.model.Geofence;
import com.lbssoft.gpsbd.model.Result;
import com.ning.http.client.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

	private static final Pattern CIRCLE = new PatternBuilder()
			.number("(-?d+.d+) ").number("(-?d+.d+), ").number("(d+.d+)").compile();

	private static final Pattern LNGLATS = new PatternBuilder()
			.number("(-?d+.d+) ").number("(-?d+.d+), ").any()
			.number("(-?d+.d+) ").number("(-?d+.d+), ")
			.number("(-?d+.d+) ").number("(-?d+.d+)")
			.compile();

	public static String getUUID(){
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	public static boolean isNumeric(String str){ 
		   Pattern pattern = Pattern.compile("[0-9]*"); 
		   Matcher isNum = pattern.matcher(str);
		   if( !isNum.matches() ){
		       return false; 
		   } 
		   return true; 
		}

	/**
	 * 判断是否一样 false 不一样
	 * @param o
	 * @param n
     * @return
     */
	public static boolean compareMap(Map<String, Object> o, Map<String, Object> n){
		for(Map.Entry<String, Object> entry:n.entrySet()){
			String nvalue = entry.getValue() == null?"":entry.getValue().toString();
			Object ovalue = o.get(entry.getKey())==null?"":o.get(entry.getKey()).toString();
			if (!ovalue.equals(nvalue)) {//若两个map中相同key对应的value不相等
				return false;
			}
		}
		return true;
	}

	/**
	 * 检查围栏数据格式是否正确
	 * @param geofence
	 * @return
     */
	public static boolean geofenceDataCheck(Geofence geofence){
		if(geofence.getType().equals("circle")){
			Parser parser = new Parser(CIRCLE, geofence.getArea());
			if(!parser.matches()){
				return false;
			}
		}else if(geofence.getType().equals("polygon")){
			Parser parser = new Parser(LNGLATS, geofence.getArea());
			if(!parser.matches()){
				return false;
			}
		}
		return true;
	}


	/**
	 * 判断字符串是否为图片URL
	 */
	public static boolean isHttpImgUrl(String urls) {
		boolean isurl = false;
		String regex = "http://(?!(\\.jpg|\\.png)).+?(\\.jpg|\\.png)";//设置正则表达式

		Pattern pat = Pattern.compile(regex.trim());//比对
		Matcher mat = pat.matcher(urls.trim());
		isurl = mat.matches();//判断是否匹配
		if (isurl) {
			isurl = true;
		}
		return isurl;
	}

	/**
	 * 发送系统命令行指令
	 * @param cmd
	 * @return
     */
	public static boolean runCommand(String cmd) {
		Log.info(cmd);
		//默认win命令
		String[] oscmd = new String[] { "cmd.exe","/c", cmd};
		if(isOSLinux()){
			oscmd = new String[] { "/bin/sh","-c", cmd};
		}
		try {
			Process process = Runtime.getRuntime().exec(oscmd);
			if (process.waitFor() == 0) {// 0 表示线程正常终止。
				return true;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return false;
	}


	/**
	 * 判断是否是linux系统
	 * @return
     */
	public static boolean isOSLinux() {
		Properties prop = System.getProperties();

		String os = prop.getProperty("os.name");
		if (os != null && os.toLowerCase().indexOf("linux") > -1) {
			return true;
		} else {
			return false;
		}
	}

	public static String BaiduGeocoder(double lng,double lat) throws Exception {
		Map params = new HashMap();
		params.put("ak",Constant.BAIDU_MAP_KEY);
		params.put("coordtype","wgs84ll");
		params.put("location",lat+","+lng);
		params.put("output","json");
		Result result = HttpUtil.get("http://api.map.baidu.com/geocoder/v2/",params);
		JSONObject obj = JSONObject.parseObject(result.getData().toString());
		JSONObject res = (JSONObject) obj.get("result");
		String address = res.getString("formatted_address")+res.getString("sematic_description");
		return address;
	}

	public  static  void main(String[] agrs) throws Exception{
		String str = BaiduGeocoder(121.11381777777778,23.129822222222224);
		System.out.println(str);
	}

}
